<?php
class Administrator_artist_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }	
	
	function content_management_add()
	{
		$is_publish = $this->input->post('is_publish');
		$artist_publish_date = $this->input->post('artist_publish_date');
		$media_type_id = $this->input->post('media_type_id');
		$genre_id = $this->input->post('genre_id');
		$genre_tag = $this->input->post('genre_tag');
		$label_id = $this->input->post('label_id');
		$is_show_recommend = $this->input->post('is_show_recommend');
		$is_show_genre = $this->input->post('is_show_genre');
		$is_show_dream_ticket = $this->input->post('is_show_dream_ticket');
		$is_show_archanian = $this->input->post('is_show_archanian');
		$letters = $this->input->post('letters');
		$artist_name = $this->input->post('artist_name');
		$artist_name_alias = $this->input->post('artist_name_alias');
		$artist_short_desc = $this->input->post('artist_short_desc');
		$artist_desc = $this->input->post('artist_desc');
		$artist_other_desc = $this->input->post('artist_other_desc');
		$artist_tag = $this->input->post('artist_tag');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$datetime = date('Y-m-d H:i:s');
		
		//Insert artist
		$data = array(
			'media_type_id' => $media_type_id,
			'artist_publish_date' => convertDatetime($artist_publish_date),
			'label_id' => $label_id,
			'is_publish' => $is_publish,
			'is_show_recommend' => $is_show_recommend,
			'is_show_genre' => $is_show_genre,
			'is_show_dream_ticket' => $is_show_dream_ticket,
			'is_show_archanian' => $is_show_archanian,
			'letters' => $letters,
			'update_by' => $this->session->userdata('username'),
			'create_date' => $datetime,
			'update_date' => $datetime
		);
		$this->db->insert('artist',$data);
		$artist_id = $this->db->insert_id();
		
		//Insert artist_info
		$data = array(
			'artist_id' => $artist_id,
			'artist_name' => $artist_name,
			'artist_name_alias' => $artist_name_alias,
			'artist_desc' => $artist_desc,
			'artist_short_desc' => $artist_short_desc,
			'artist_other_desc' => $artist_other_desc,
			'genre_tag' => $genre_tag,
			'artist_tag' => $artist_tag
		);
		$this->db->insert('artist_info',$data);	
		
		//Insert artist_image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $artist_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'artist/images/',
				'dimension' => array(
					array(
						'width' => 120,
						'height' => 120
					)			
				)
			));		
			
			//delete temp
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(
				'artist_id' => $artist_id,
				'artist_file_name' => $data->file_name,
				'artist_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('artist_image',$dataImage);	
		
		/*Insert artist_genre*/
		$dataArtistGenre = array();
		if(!empty($genre_id)) foreach($genre_id as $value):
			$dataArtistGenre[] = array(
				'genre_id' => $value,
				'artist_id' => $artist_id
			);
		endforeach;
		if(!empty($dataArtistGenre)) $this->db->insert_batch('artist_genre',$dataArtistGenre);			
		
		//Update Solr
		$this->load->library('solr');	
			
		$document = new Apache_Solr_Document();
		$document->id = 'artist_'.$artist_id;
		$document->artist_name_s = $artist_name;	
		$document->artist_short_desc_s = $artist_short_desc;
		$document->artist_tag_s = $artist_tag;
		$document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime($datetime));	
		
		if(!empty($dataImage)):
			$document->artist_image_name_s = $dataImage[0]['artist_file_name'].'.'.$dataImage[0]['artist_file_ext'];
		endif;
		
		if($this->solr->ping() && !empty($document)):
			$this->solr->addDocument($document);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;
		
		//Clear Memcache
		$this->memcache->delete('dataArtistList');
		$this->memcache->delete('dataArtistNewRelease');
		$this->memcache->delete('dataArtistRecommend');
		$this->memcache->delete('dataDreamTicket');
		$this->memcache->delete('dataHomeDreamTicket');
	}
	
	function content_management_edit($artist_id)
	{
		$is_publish = $this->input->post('is_publish');
		$artist_publish_date = $this->input->post('artist_publish_date');
		$media_type_id = $this->input->post('media_type_id');
		$genre_id = $this->input->post('genre_id');
		$genre_tag = $this->input->post('genre_tag');
		$label_id = $this->input->post('label_id');
		$is_show_recommend = $this->input->post('is_show_recommend');
		$is_show_genre = $this->input->post('is_show_genre');
		$is_show_dream_ticket = $this->input->post('is_show_dream_ticket');
		$is_show_archanian = $this->input->post('is_show_archanian');
		$letters = $this->input->post('letters');
		$artist_name = $this->input->post('artist_name');
		$artist_name_alias = $this->input->post('artist_name_alias');
		$artist_short_desc = $this->input->post('artist_short_desc');
		$artist_desc = $this->input->post('artist_desc');
		$artist_other_desc = $this->input->post('artist_other_desc');
		$artist_tag = $this->input->post('artist_tag');
		$uploadify_store = explode(',',$this->input->post('uploadify_store'));
		$datetime = date('Y-m-d H:i:s');
		
		/*Update user*/
		$data = array(
			'media_type_id' => $media_type_id,
			'artist_publish_date' => convertDatetime($artist_publish_date),
			'label_id' => $label_id,
			'is_publish' => $is_publish,
			'is_show_recommend' => $is_show_recommend,
			'is_show_genre' => $is_show_genre,
			'is_show_dream_ticket' => $is_show_dream_ticket,
			'is_show_archanian' => $is_show_archanian,
			'letters' => $letters,
			'update_by' => $this->session->userdata('username'),
			'update_date' => $datetime
		);
		$this->db->update('artist',$data,array('artist_id' => $artist_id));
		
		/*Update artist_info*/
		$data = array(
			'artist_id' => $artist_id,
			'artist_name' => $artist_name,
			'artist_name_alias' => $artist_name_alias,
			'artist_desc' => $artist_desc,
			'artist_short_desc' => $artist_short_desc,
			'artist_other_desc' => $artist_other_desc,
			'genre_tag' => $genre_tag,
			'artist_tag' => $artist_tag
		);
		$this->db->update('artist_info',$data,array('artist_id' => $artist_id));
		
		//Insert artist_image
		$dataImage = array();
		if(!empty($uploadify_store[0])) foreach($uploadify_store as $key => $value):
			//Resize Image
			$data = imageResize(array(
				'id' => $artist_id,
				'source_image' => $this->temp_folder.$value,
				'new_path' => $this->upload_folder.'artist/images/',
				'dimension' => array(
					array(
						'width' => 120,
						'height' => 120
					)					
				)
			));		
			
			//temp image
			@unlink($this->temp_folder.$value);
			
			$dataImage[] = array(
				'artist_id' => $artist_id,
				'artist_file_name' => $data->file_name,
				'artist_file_ext' => $data->file_ext
			);	
		endforeach;
		if(!empty($dataImage)) $this->db->insert_batch('artist_image',$dataImage);
		
		/*Insert artist_genre*/
		$dataArtistGenre = array();
		$this->db->delete('artist_genre',array('artist_id' => $artist_id)); //Delete First
		if(!empty($genre_id)) foreach($genre_id as $value):
			$dataArtistGenre[] = array(
				'genre_id' => $value,
				'artist_id' => $artist_id
			);
		endforeach;
		if(!empty($dataArtistGenre)) $this->db->insert_batch('artist_genre',$dataArtistGenre);		
		
		//Update Solr
		$this->load->library('solr');	
			
		$document = new Apache_Solr_Document();
		$document->id = 'artist_'.$artist_id;
		$document->artist_name_s = $artist_name;	
		$document->artist_short_desc_s = $artist_short_desc;
		$document->artist_tag_s = $artist_tag;
		$document->last_modified = date("Y-m-d\TH:i:s\Z",strtotime($datetime));
		
		$dataImage = $this->db->select('artist_file_name,artist_file_ext')->order_by('artist_image_order ASC,artist_image_id ASC')->get_where('artist_image',array('artist_id' => $artist_id),1)->row();
		if(!empty($dataImage->artist_file_name)) $document->artist_image_name_s = $dataImage->artist_file_name.'.'.$dataImage->artist_file_ext;
		
		if($this->solr->ping() && !empty($document)):
			$this->solr->addDocument($document);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;	
		
		//Clear Memcache
		$this->memcache->delete('dataArtistList');
		$this->memcache->delete('dataArtistNewRelease');
		$this->memcache->delete('dataArtistRecommend');
		$this->memcache->delete('dataDreamTicket');
		$this->memcache->delete('dataHomeDreamTicket');	
	}
	
	function ajax_update_image()
	{
		$artist_image_id = $this->input->post('artist_image_id');
		if(!empty($artist_image_id)) foreach($artist_image_id as $key => $value):
			$this->db->update('artist_image',array('artist_image_order' => $key),array('artist_image_id' => $value));
		endforeach;
		exit;
	}
	
	function ajax_delete_image($artist_image_id)
	{
		$dataImage = $this->db->get_where('artist_image',array('artist_image_id' => $artist_image_id),1)->row();
		$this->db->delete('artist_image',array('artist_image_id' => $artist_image_id));
		//Delete All Image
		$map = directory_map($this->upload_folder.'artist/images/', 1);
		foreach($map as $folder):
			@unlink($this->upload_folder.'artist/images/'.$folder.'/'.$dataImage->artist_file_name.'.'.$dataImage->artist_file_ext);
		endforeach;
		exit;
	}		
	
	function content_management_delete()
	{
		$id = $this->input->post('id');
		$map = directory_map($this->upload_folder.'artist/images/', 1);
		$ids = array();
		foreach($id as $value):
			$ids[] = "artist_".$value;
			$dataImage = $this->db->get_where('artist_image',array('artist_id' => $value))->result();
			$tables = array('artist','artist_info','artist_image','artist_click_stat','song_artist','video_artist','news_artist');
			$this->db->where('artist_id', $value);
			$this->db->delete($tables);
			//Delete All Image
			foreach($map as $folder):
				foreach($dataImage as $items):
					@unlink($this->upload_folder.'artist/images/'.$folder.'/'.$items->artist_file_name.'.'.$items->artist_file_ext);
				endforeach;
			endforeach;					
		endforeach;	
		
		//Update Solr
		$this->load->library('solr');	
		if($this->solr->ping() && !empty($ids)):
			$this->solr->deleteByMultipleIds($ids);
			$this->solr->commit(); //commit to see the deletes and the document
			$this->solr->optimize(); //merges multiple segments into one
		endif;	
		
		//Clear Memcache
		$this->memcache->delete('dataArtistList');
		$this->memcache->delete('dataArtistNewRelease');
		$this->memcache->delete('dataArtistRecommend');			
	}
}

?>